﻿
Imports ESRI.ArcGIS.Geodatabase
Imports ESRI.ArcGIS.Carto
Imports ESRI.ArcGIS.DataSourcesFile
Imports ESRI.ArcGIS.Geometry
Imports ESRI.ArcGIS.Display
Imports System.Runtime.InteropServices
Imports ESRI.ArcGIS.esriSystem

Public Class WelcomeForm
    'Public userDAO As UserDAO = New UserDAO
    Public siteDAO As SiteDAO = New SiteDAO
    Dim sites As List(Of Site)
    Public actionsList As Object = New Object

    Public loaded As Boolean = False
    Private Sub addSiteLabel_LinkClicked(ByVal sender As System.Object, ByVal e As System.Windows.Forms.LinkLabelLinkClickedEventArgs) Handles addSiteLabel.LinkClicked
        Dim newSite As New SiteNouv
        newSite.WindowState = FormWindowState.Maximized
        newSite.ShowDialog(Me)
        displayListSites()
    End Sub

    'Private Sub folderDataLabel_LinkClicked(ByVal sender As System.Object, ByVal e As System.Windows.Forms.LinkLabelLinkClickedEventArgs) Handles folderDataLabel.LinkClicked
    '    Dim workSpaceConfig As New WorkspacesConfig
    '    workSpaceConfig.ShowDialog(Me)
    'End Sub


    'Private Sub LinkLabel1_LinkClicked(ByVal sender As System.Object, ByVal e As System.Windows.Forms.LinkLabelLinkClickedEventArgs)

    'End Sub

    Public Sub displayAllSitesOnMap()
        If sites Is Nothing Then
            Return
        End If
        For Each site As Site In sites
            drawOneSiteOnMap(site, 5)
        Next
    End Sub

    Private Sub drawOneSiteOnMap(ByVal site As Site, ByVal OutlineSize As Integer)
        Try
            Dim pt As Point = New Point()
            pt.X = (CDbl(site.LONE_SIT) + CDbl(site.LONW_SIT)) / 2
            pt.Y = (CDbl(site.LATS_SIT) + CDbl(site.LATN_SIT)) / 2
            CreateMarkerElement(pt, OutlineSize)
        Catch ex As Exception

        End Try
    End Sub
    Public Sub CreateMarkerElement(ByVal point As IPoint, ByVal OutlineSize As Integer)
        Dim m_element As IElement
        Dim activeView As IActiveView = carteMarocLocal.ActiveView
        Dim graphicsContainer As IGraphicsContainer = TryCast(carteMarocLocal.Map, IGraphicsContainer)

        Dim markerElement As IMarkerElement = TryCast(New MarkerElement(), IMarkerElement)
        Dim simpleMarkerSymbol As ISimpleMarkerSymbol = New SimpleMarkerSymbol()

        simpleMarkerSymbol.Color = getRGB(255, 0, 0)
        simpleMarkerSymbol.Outline = True
        simpleMarkerSymbol.OutlineColor = TryCast(getRGB(0, 255, 0), IColor)
        simpleMarkerSymbol.OutlineSize = OutlineSize
        simpleMarkerSymbol.Style = esriSimpleMarkerStyle.esriSMSCircle
        Dim symbol As ISymbol = TryCast(simpleMarkerSymbol, ISymbol)
        symbol.ROP2 = esriRasterOpCode.esriROPNotXOrPen
        markerElement.Symbol = simpleMarkerSymbol
        m_element = TryCast(markerElement, IElement)
        m_element.Geometry = TryCast(point, IGeometry)
        graphicsContainer.AddElement(m_element, 0)
        activeView.PartialRefresh(esriViewDrawPhase.esriViewGraphics, m_element, Nothing)
        Dim geometry As IGeometry = m_element.Geometry
        'MovePointFeedback.Display = activeView.ScreenDisplay
        'MovePointFeedback.Symbol = TryCast(simpleMarkerSymbol, ISymbol)
        'MovePointFeedback.Start(TryCast(geometry, IPoint), point)
    End Sub
    Private Function getRGB(ByVal r As Integer, ByVal g As Integer, ByVal b As Integer) As IRgbColor
        Dim pColor As IRgbColor = New RgbColorClass()
        pColor.Red = r
        pColor.Green = g
        pColor.Blue = b
        Return pColor
    End Function
    Private Sub WelcomeForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim canAddSite As Boolean = False
        For Each action In actionsList
            If action.action_name = Actions.config_action Then
                canAddSite = True
                Exit For
            End If
        Next

        addSiteIcon.Visible = canAddSite
        addSiteLabel.Visible = canAddSite

        Try
            loadCarteMarocLocal()
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try


        displayListSites()
        displayAllSitesOnMap()


    End Sub

    Sub displayListSites()
        sites = siteDAO.getAllSite()
        ListSites.DataSource = sites
        ListSites.DisplayMember = "NOMSITE"
    End Sub

    Private Sub loadCarteMarocLocal()
        ' cartetab.SelectedIndex = 1
        ' Create a new ShapefileWorkspaceFactory object and
        ' open a shapefile folder - the path works with standard 9.3 installation
        Try
            ' Create a new ShapefileWorkspaceFactory object and
            ' open a shapefile folder - the path works with standard 9.3 installation
            Dim delimitationPath As String = getSiteDelimitationsPath()

            Dim workspaceFactory As IWorkspaceFactory = New ShapefileWorkspaceFactory()
            Dim featureWorkspace As IFeatureWorkspace = TryCast(workspaceFactory.OpenFromFile(delimitationPath, 0), IFeatureWorkspace)
            ' Create a new FeatureLayer and assign a shapefile to it
            Dim featureLayer As IFeatureLayer = New FeatureLayerClass
            featureLayer.FeatureClass = featureWorkspace.OpenFeatureClass("COMMUNES")
            Dim layer As ILayer = featureLayer
            layer.Name = featureLayer.FeatureClass.AliasName
            ' Add the Layer to the focus map
            Dim pMap As ESRI.ArcGIS.Carto.IMap = carteMarocLocal.ActiveView.FocusMap
            pMap.AddLayer(featureLayer)
        Catch ex As COMException
            MsgBox(ex.StackTrace)
        End Try

    End Sub

    Private Sub displaySiteWS(ByVal codeSite As String)
        If TryCast(Me.ParentForm, Main) IsNot Nothing Then
            Dim mainApps As Main = TryCast(Me.ParentForm, Main)
            If mainApps.workspaces.ContainsKey(codeSite) Then
                Dim workspaceTemp As Workspace = mainApps.workspaces.Item(codeSite)
                If workspaceTemp.removed Then
                    mainApps.workspaces.Remove(codeSite)
                    mainApps.openWorkspace(codeSite)
                End If
            Else
                mainApps.openWorkspace(codeSite)
            End If
        End If
    End Sub
    Private Sub ListSites_MouseDoubleClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles ListSites.MouseDoubleClick
        Dim listSender As ListBox = TryCast(sender, ListBox)
        Cursor = Cursors.WaitCursor
        If listSender IsNot Nothing Then
            Dim selectedSite As Site = listSender.SelectedItem
            displaySiteWS(selectedsite.CODSITE)
        End If
        Cursor = Cursors.Default
    End Sub

    'Private Sub onStartupConfigLink_LinkClicked(ByVal sender As System.Object, ByVal e As System.Windows.Forms.LinkLabelLinkClickedEventArgs) Handles onStartupConfigLink.LinkClicked
    '    Dim startupFrm As AppStartup = New AppStartup()
    '    startupFrm.ShowDialog(Me)
    'End Sub

    Public Sub displayConfiguredSites()
        If Settings.Default.DisplayedSitesOnStartup Is Nothing OrElse Settings.Default.DisplayedSitesOnStartup.Trim().Length = 0 Then
            Return
        End If
        If loaded Then
            Return
        End If

        loaded = True
        Cursor = Cursors.WaitCursor
        sites = siteDAO.getAllSite()
        Dim tokens As String() = Settings.Default.DisplayedSitesOnStartup.Split(";")
        For Each site As Site In sites
            If site.CODSITE IsNot Nothing And site.CODSITE.Trim().Length > 0 And tokens.Contains(site.CODSITE) Then
                displaySiteWS(site.CODSITE)
            End If
        Next
        'Me.SendToBack()
        Cursor = Cursors.Default
    End Sub

    Private Sub ListSites_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListSites.SelectedIndexChanged
        carteMarocLocal.ActiveView.GraphicsContainer.DeleteAllElements()
        If TryCast(ListSites.SelectedItem, Site) Is Nothing Then
            displayAllSitesOnMap()
            Return
        End If
        For Each site As Site In sites
            If TryCast(ListSites.SelectedItem, Site).CODSITE <> site.CODSITE Then
                drawOneSiteOnMap(site, 5)
            End If
        Next
        drawOneSiteOnMap(ListSites.SelectedItem, 0)
        'selectedSiteTimer.Interval = 10000
        'selectedSiteTimer.Start()

        'selectedSiteTimerHide.Interval = 20000
        'selectedSiteTimerHide.Start()
    End Sub

    Private Sub selectedSiteTimer_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles selectedSiteTimer.Tick

        If TryCast(ListSites.SelectedItem, Site) Is Nothing Then
            carteMarocLocal.ActiveView.GraphicsContainer.DeleteAllElements()
            displayAllSitesOnMap()
            Return
        End If

    End Sub

    Private Sub selectedSiteTimerHide_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles selectedSiteTimerHide.Tick
        carteMarocLocal.ActiveView.GraphicsContainer.DeleteAllElements()
        If TryCast(ListSites.SelectedItem, Site) Is Nothing Then
            displayAllSitesOnMap()
            Return
        End If
        For Each site As Site In sites
            If TryCast(ListSites.SelectedItem, Site).CODSITE <> site.CODSITE Then
                drawOneSiteOnMap(site, 5)
            End If
        Next
        drawOneSiteOnMap(ListSites.SelectedItem, 0)
    End Sub

    'Private Sub availabaleLayers_LinkClicked(ByVal sender As System.Object, ByVal e As System.Windows.Forms.LinkLabelLinkClickedEventArgs) Handles availabaleLayers.LinkClicked
    '    Dim configLayers As New layersSiteConfigUI()
    '    configLayers.ShowDialog(Me)
    'End Sub

   
   
End Class